﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKRevitBase.WinForm;
using YRevitJig;
using YJKRevitView;
using RevitInteractiveUtility.ExternalEventManager;

namespace YJKRevitDimension.PlaneSlopeDimension
{
	[Transaction(TransactionMode.Manual)]
	internal class CmdAddPlaneSlopeDim : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)9;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			SlopeDimensionStructCol slopeDimensionStructCol = new SlopeDimensionStructCol(commandData);
			slopeDimensionStructCol.Init();
			if (CmdAddPlaneSlopeDim._hWndRevit == null)
			{
				CmdAddPlaneSlopeDim._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			SlopeDimensionForm slopeDimensionForm = new SlopeDimensionForm(slopeDimensionStructCol, CmdAddPlaneSlopeDim._hWndRevit, document);
			slopeDimensionForm.Show(CmdAddPlaneSlopeDim._hWndRevit);
			int wMsg = 7;
			CmdAddPlaneSlopeDim.SendMessage(CmdAddPlaneSlopeDim._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			XLineJig xlineJig = null;
			for (;;)
			{
				int num = 0;
				try
				{
					xlineJig = new XLineJig(activeUIDocument);
					xlineJig.ResetPoint = XUiJigBase.EResetPoint.eByAxis;
					XYZ xyz = selection.PickPoint("请选择坡度标注第一点");
					if (xyz == null)
					{
						num = 1;
						xlineJig.Dispose();
						slopeDimensionForm.Close();
					}
					else
					{
						XYZ xyz2 = xlineJig.PickEndPoint(xyz, "请选择坡度标注第二点");
						if (xyz2 == null)
						{
							num = 2;
							xlineJig.Dispose();
							continue;
						}
						SlopeDimensionStruct slopeDimensionStruct;
						if (!this.GetSlopeDimStructByName(slopeDimensionForm.ArrowType, slopeDimensionStructCol, out slopeDimensionStruct))
						{
							continue;
						}
						slopeDimensionStruct.UpText = slopeDimensionForm.LineUpText;
						slopeDimensionStruct.DownText = slopeDimensionForm.LineDownText;
						slopeDimensionStruct.Font = slopeDimensionForm.FontType;
						slopeDimensionStruct.TextHeight = double.Parse(slopeDimensionForm.TextHeight);
						slopeDimensionStruct.TextWidth = double.Parse(slopeDimensionForm.TextWidth);
						slopeDimensionStruct.ArrowAngle = double.Parse(slopeDimensionForm.ArrowAngle);
						slopeDimensionStruct.ArrowLength = double.Parse(slopeDimensionForm.ArrowLength);
						slopeDimensionStruct.Offset = double.Parse(slopeDimensionForm.Offset);
						slopeDimensionStruct.Length = this.GetLength(slopeDimensionForm.LineUpText, slopeDimensionForm.LineDownText);
						SlopeDimesionOper.AddSlopeDimToDoc(new SlopeDimensionData(commandData, slopeDimensionStruct, xyz2, xyz));
						continue;
					}
				}
				catch (Exception)
				{
					if (num == 2)
					{
						continue;
					}
					slopeDimensionForm.Close();
				}
				finally
				{
					if (xlineJig != null)
					{
						xlineJig.Dispose();
					}
				}
				break;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void InitExternalEventRequest()
		{
			ExternalEventManagerSingleton eventManger = ExternalEventManagerSingleton.GetEventManger(false);
			AddSlopeDimensionRequest appendValue = new AddSlopeDimensionRequest(1);
			eventManger.AppendRequest(appendValue);
		}

		private double GetLength(string name1, string name2)
		{
			int num = (int)Math.Ceiling((SlopeDimesionOper.ReadTextMaxLenth(name1, name2) - 6.0) / 2.0);
			return (double)(8 + num);
		}

		private bool GetSlopeDimStructByName(string name, SlopeDimensionStructCol slopeDimStructCol, out SlopeDimensionStruct slopeDimStruct)
		{
			if (slopeDimStructCol.LstSlopeDimensionStruct.Count < 0)
			{
				slopeDimStruct = null;
				return false;
			}
			foreach (SlopeDimensionStruct slopeDimensionStruct in slopeDimStructCol.LstSlopeDimensionStruct)
			{
				if (slopeDimensionStruct.Name == name)
				{
					slopeDimStruct = slopeDimensionStruct;
					return true;
				}
			}
			slopeDimStruct = null;
			return false;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private static WindowHandle _hWndRevit;

		private enum ERequestId
		{
			eAddSlopeDim = 1,
			eNoOper
		}
	}
}
